﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.IO;
using Yipusoft.SoldierArchives.BLL;
using Yipusoft.SoldierArchives.Model;

public partial class BasicBase_SoldierArchive_Print : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            if (Session["Where"] != null)
            {
                string Where = Session["Where"].ToString();
                lblWhere.Text = Where;
                Session.Remove("Where");

                init();
            }
            else
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "PrintDutyScript()", "<script type='text/javascript'>alert('请重新查询');</script>");
            }
        }
    }

    void init()
    {

        string Where = lblWhere.Text;
        NewSoldier soldier = new NewSoldier();
        int count = soldier.GetSoldiersCountByWhere(Where);
        gvSoldiers.DataSource = soldier.GetSoldiersByWhere(Where, "DepartmentID", "ASC", count, 0);
        gvSoldiers.DataBind();
    }

    protected void lkbExcel_Click(object sender, EventArgs e)
    {
        ExportData("application/ms-excel", DateTime.Now.ToString("yyMdHm") + ".xls");
    }

    private void ExportData(string FileType, string FileName)
    {
        Response.Charset = "GB2312";
        Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");

        Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
        Response.ContentType = FileType;
        this.EnableViewState = false;
        StringWriter tw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(tw);
        gvSoldiers.RenderControl(hw);
        Response.Write(tw.ToString());
        Response.End();
    }
    public override void VerifyRenderingInServerForm(Control control)
    {
        //base.VerifyRenderingInServerForm(control);
    }

    protected void gvSoldiers_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header)
        {
            TableCellCollection tcl = e.Row.Cells;
            foreach (TableCell cell in tcl)
            {
                cell.Attributes.Add("style", "white-space:nowrap");
            }
        }
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            TableCellCollection tcl = e.Row.Cells;
            foreach (TableCell cell in tcl)
            {
                cell.Attributes.Add("style", "white-space:nowrap");
            }
            Label ID = (Label)e.Row.FindControl("ID");
            ID.Text = (e.Row.RowIndex + 1).ToString();
            //当鼠标放上去的时候 先保存当前行的背景颜色 并给附一颜色 
            e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#F5F5F5',this.style.fontWeight='';");
            //当鼠标离开的时候 将背景颜色还原的以前的颜色 
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor,this.style.fontWeight='';");
        }
    }

    public string GetCodeItem(object ItemID)
    {
        if (ItemID.ToString() == "")
            return "--";
        else
        {
            Yipusoft.Common.Model.CodeItemInfo info = Yipusoft.Common.BLL.CodeItem.GetCodeItem(ItemID.ToString());
            if (info != null)
            {
                return info.ItemName;
            }
            else
            {
                return "--";
            }
        }
    }

    public string GetDateTime(object date)
    {
        DateTime dt = Convert.ToDateTime(date);
        if (dt.Year == 9999)
            return "--";
        else
        {
            return dt.ToString("yyyy-MM-dd");
        }
    }

    public string GetBool(object data)
    {
        bool isC = Convert.ToBoolean(data);
        if (isC)
            return "是";
        else
            return "否";
    }

    public string GetDepartment(object data)
    {
        if (data.ToString() != "")
        {
            Yipusoft.Account.Model.DepartmentInfo department = Yipusoft.Account.BLL.Department.GetDepartment(data.ToString());
            if (department != null)
            {
                return department.FullName;
            }
            else
            {
                return "--";
            }
        }
        else
        {
            return "--";
        }

    }
}
